package com.cx.update;

import android.content.Context;
import android.widget.ImageView;
import android.widget.TextView;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class DownloadUtil {
    
    public static final int DOWNLOAD_FILE_EXIST = 0;
    
    public static final int DOWNLOAD_FAILED = 1;
    
    public static final int DOWNLOAD_SD_CARD_ERROR = 2;
    
	public static int getProgress(int hascount, int length) {
		return (int) (((float) hascount / length) * 100);
	}

	private static void closeInStream(InputStream in){
		try {
			if(in != null){
				in.close();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	private static void closeOutStream(OutputStream out){
		try {
			if(out != null){
				out.close();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

    public static boolean downloadFile(Context mContext, String url, File file,
            RecomDownloadListener listener, TextView tv) {
        InputStream in = null;
        BufferedInputStream bis = null;
        FileOutputStream fos = null;
        BufferedOutputStream bos = null;
        try {
           
            HttpURLConnection conn = (HttpURLConnection)new URL(url).openConnection();
            conn.setDoInput(true);
            conn.connect();
            in = conn.getInputStream();
            int length = conn.getContentLength();
            FileUtils.saveIntData(mContext, file.getName(), length);
            bis = new BufferedInputStream(in);
            fos = new FileOutputStream(file);
            bos = new BufferedOutputStream(fos);
            int count = 0;
            byte buf[] = new byte[1024 * 10]; // 缓存
            int numread = 0;
            int progress = 0;
            int oldProgress = 0;
            while ((numread = bis.read(buf)) > 0) {
                count += numread;
                progress = getProgress(count, length);
                if (progress - oldProgress > 1) {
                    if (null != listener) {
                        listener.step(tv, progress);
                    }
                    oldProgress = progress;
                }
                bos.write(buf, 0, numread);// 写入文件
            }
            bos.flush();
            return true;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            closeInStream(in);
            closeInStream(bis);
            closeOutStream(bos);
            closeOutStream(fos);
        }
        return false;
    }
    
    public static boolean downloadFile(Context mContext, String url, File file,
            ApkDownloadListener listener) {
        InputStream in = null;
        BufferedInputStream bis = null;
        FileOutputStream fos = null;
        BufferedOutputStream bos = null;
        try {
            
            HttpURLConnection conn = (HttpURLConnection)new URL(url).openConnection();
            conn.setDoInput(true);
            conn.connect();
            in = conn.getInputStream();
            int length = conn.getContentLength();
            FileUtils.saveIntData(mContext, file.getName(), length);
            bis = new BufferedInputStream(in);
            fos = new FileOutputStream(file);
            bos = new BufferedOutputStream(fos);
            int count = 0;
            byte buf[] = new byte[1024 * 10]; // 缓存
            int numread = 0;
            int progress = 0;
            int oldProgress = 0;
            while ((numread = bis.read(buf)) > 0) {
                count += numread;
                progress = getProgress(count, length);
                if (progress - oldProgress > 1) {
                    if (null != listener) {
                        listener.step(progress);
                    }
                    oldProgress = progress;
                }
                bos.write(buf, 0, numread);// 写入文件
            }
            bos.flush();
            return true;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            closeInStream(in);
            closeInStream(bis);
            closeOutStream(bos);
            closeOutStream(fos);
        }
        return false;
    }
    
    public interface ApkDownloadListener {
        void start();
        void step(int step);
        void downloadSuccess();
        void downloadError(int result);
    }

	public interface RecomDownloadListener {
        void start(TextView v);
        void step(TextView v, int step);
        void downloadSuccess(TextView v, ImageView iv);
        void downloadError(TextView v, int result);
    }
}


